import os
import shutil

def move_files_up():
    # 获取当前工作目录
    root_dir = os.getcwd()
    
    # 遍历所有目录
    for root, dirs, files in os.walk(root_dir, topdown=False):
        # 检查当前目录名是否包含"hf-mirror.com"
        if "hf-mirror.com" in root:
            parent_dir = os.path.dirname(root)
            print(f"发现目标目录: {root}")
            
            # 移动所有文件到上层目录
            for file in files:
                source_path = os.path.join(root, file)
                dest_path = os.path.join(parent_dir, file)
                
                try:
                    # 如果目标文件已存在，添加数字后缀
                    if os.path.exists(dest_path):
                        base, ext = os.path.splitext(file)
                        counter = 1
                        while os.path.exists(dest_path):
                            new_name = f"{base}_{counter}{ext}"
                            dest_path = os.path.join(parent_dir, new_name)
                            counter += 1
                    
                    # 移动文件
                    shutil.move(source_path, dest_path)
                    print(f"已移动: {file} -> {dest_path}")
                except Exception as e:
                    print(f"移动文件 {file} 时出错: {str(e)}")

if __name__ == "__main__":
    try:
        move_files_up()
        print("文件移动完成！")
    except Exception as e:
        print(f"程序执行出错: {str(e)}")
    
    # 防止CMD窗口闪退
    input("\n按回车键退出...")
